﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLVatTu.Class;
using QLVatTu.InPhieu;

namespace QLVatTu.NghiepVu
{
    public partial class frmQuanLyYeuCauNhap : Form
    {
        KetNoiDB cn = new KetNoiDB();

        public frmQuanLyYeuCauNhap()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frmPhieuYeuCauNhap frm = new frmPhieuYeuCauNhap();
            frm.ShowDialog();
            load_dgvPhieuYeuCauNhap();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            if (TopLevel == false) this.Parent.Parent.Controls.Remove(this.Parent);
            else this.Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if(dgvPhieuYeuCauNhap.SelectedRows.Count>0 && dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value!=null)
            {
                bool TrangThai = bool.Parse(cn.LayDuLieu("select TrangThai from tbl_PhieuYeuCauNhap where SoPhieu='"+dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString()+"'"));
                if (TrangThai == true)
                {
                    MessageBox.Show("Không thể sửa phiếu đã duyệt.","Thông báo",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    return;
                }
                frmPhieuYeuCauNhap frm = new frmPhieuYeuCauNhap();
                frm.MaPhieuYeuCau=dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                frm.ShowDialog();
                load_dgvPhieuYeuCauNhap();
            }
        }

        private void loadData()
        {
            cbbCongTrinhDuAn.DataSource = cn.getTable("select * from tbl_CongTrinhDuAn");
            cbbCongTrinhDuAn.DisplayMember = "TenCongTrinhDuAn";
            cbbCongTrinhDuAn.ValueMember = "MaCongTrinhDuAn";

            cbbTrangThai.DataSource = cn.CreateTrangThai();
            cbbTrangThai.DisplayMember = "TenTrangThai";
            cbbTrangThai.ValueMember = "TrangThai";

            load_dgvPhieuYeuCauNhap();
        }

        private void load_dgvPhieuYeuCauNhap(string dieuKien = null)
        {
            string sql="";
            if (dieuKien != null)
            {
                sql = @"SELECT     dbo.tbl_PhieuYeuCauNhap.SoPhieu, dbo.tbl_PhieuYeuCauNhap.NgayLap, dbo.tbl_PhieuYeuCauNhap.NguoiLap, dbo.tbl_CongTrinhDuAn.TenCongTrinhDuAn, 
                                              dbo.tbl_PhieuYeuCauNhap.MaPhieuDuTru, dbo.tbl_PhieuYeuCauNhap.TrangThai
                        FROM         dbo.tbl_PhieuYeuCauNhap INNER JOIN
                                              dbo.tbl_CongTrinhDuAn ON dbo.tbl_PhieuYeuCauNhap.MaCongTrinhDuAn = dbo.tbl_CongTrinhDuAn.MaCongTrinhDuAn
                        " + dieuKien;
            }
            else
            {
                sql = @"SELECT     dbo.tbl_PhieuYeuCauNhap.SoPhieu, dbo.tbl_PhieuYeuCauNhap.NgayLap, dbo.tbl_PhieuYeuCauNhap.NguoiLap, dbo.tbl_CongTrinhDuAn.TenCongTrinhDuAn, 
                                              dbo.tbl_PhieuYeuCauNhap.MaPhieuDuTru, dbo.tbl_PhieuYeuCauNhap.TrangThai
                        FROM         dbo.tbl_PhieuYeuCauNhap INNER JOIN
                                              dbo.tbl_CongTrinhDuAn ON dbo.tbl_PhieuYeuCauNhap.MaCongTrinhDuAn = dbo.tbl_CongTrinhDuAn.MaCongTrinhDuAn";
            }
            dgvPhieuYeuCauNhap.Rows.Clear();
            DataTable tbl = cn.getTable(sql);
            if (tbl.Rows.Count > 0)
            {
                foreach (DataRow dr in tbl.Rows)
                {
                    DataGridViewRow dgvr=(DataGridViewRow)dgvPhieuYeuCauNhap.Rows[0].Clone();
                    dgvr.Cells[0].Value=dr["SoPhieu"].ToString();
                    dgvr.Cells[1].Value=DateTime.Parse(dr["NgayLap"].ToString());
                    dgvr.Cells[2].Value=dr["NguoiLap"].ToString();
                    dgvr.Cells[3].Value=dr["TenCongTrinhDuAn"].ToString();
                    dgvr.Cells[4].Value=dr["MaPhieuDuTru"].ToString();
                    bool TrangThai=bool.Parse(dr["TrangThai"].ToString());
                    if (TrangThai == true)
                    {
                        dgvr.Cells[5].Value = "Đã phê duyệt";
                    }
                    else
                    {
                        dgvr.Cells[5].Value="Chưa phê duyệt";
                    }
                    dgvPhieuYeuCauNhap.Rows.Add(dgvr);
                }
            }
        }

        private void frmQuanLyYeuCauNhap_Load(object sender, EventArgs e)
        {
            if (cn.KetNoi())
            {
                loadData();
            }
        }

        private void dgvPhieuYeuCauNhap_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvPhieuYeuCauNhap.SelectedRows.Count > 0 && dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                if (dgvPhieuYeuCauNhap.CurrentRow.Cells["colTrangThai"].Value.ToString() == "Đã phê duyệt")
                {
                    btnDuyet.Text = "Hủy duyệt";
                }
                else
                {
                    btnDuyet.Text = "Phê duyệt";
                }

                string sql = @"SELECT  row_number() over(order by tbl_ChiTietPhieuYeuCauNhap.SoPhieu) as STT, dbo.tbl_VatTu.TenVatTu, dbo.tbl_DonViTinh.TenDonViTinh, dbo.tbl_ChiTietPhieuYeuCauNhap.SoLuong
                               FROM         dbo.tbl_ChiTietPhieuYeuCauNhap INNER JOIN
                                                      dbo.tbl_DonViTinh ON dbo.tbl_ChiTietPhieuYeuCauNhap.MaDonViTinh = dbo.tbl_DonViTinh.MaDonViTinh INNER JOIN
                                                      dbo.tbl_VatTu ON dbo.tbl_ChiTietPhieuYeuCauNhap.MaVatTu = dbo.tbl_VatTu.MaVatTu
                                WHERE tbl_ChiTietPhieuYeuCauNhap.SoPhieu='" + dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'";
                dgvChiTiet.Rows.Clear();
                DataTable tbl = cn.getTable(sql);
                if (tbl.Rows.Count > 0)
                {
                    foreach (DataRow dr in tbl.Rows)
                    {
                        DataGridViewRow dtChiTiet = (DataGridViewRow)dgvChiTiet.Rows[0].Clone();
                        dtChiTiet.Cells[0].Value = dr["STT"].ToString();
                        dtChiTiet.Cells[1].Value = dr["TenVatTu"].ToString();
                        dtChiTiet.Cells[2].Value = dr["TenDonViTinh"].ToString();
                        dtChiTiet.Cells[3].Value = dr["SoLuong"].ToString();
                        dgvChiTiet.Rows.Add(dtChiTiet);
                    }
                }
            }
            else
            {
                dgvChiTiet.Rows.Clear();
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            frmInPhieuYeuCauNhap frm = new frmInPhieuYeuCauNhap();
            if (dgvPhieuYeuCauNhap.SelectedRows.Count > 0 && dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                List<PhieuYeuCauNhap> list = new List<PhieuYeuCauNhap>();
                string SoPhieu=dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                string Ngay = "";
                string Thang = "";
                string Nam = "";
                string NguoiLap = "";
                string CongTrinhDuAn = "";

                string sql = "";
                DataTable dt = new DataTable();
                sql = @"SELECT     dbo.tbl_PhieuYeuCauNhap.NgayLap, dbo.tbl_PhieuYeuCauNhap.NguoiLap, dbo.tbl_CongTrinhDuAn.TenCongTrinhDuAn
                        FROM         dbo.tbl_PhieuYeuCauNhap INNER JOIN
                                              dbo.tbl_CongTrinhDuAn ON dbo.tbl_PhieuYeuCauNhap.MaCongTrinhDuAn = dbo.tbl_CongTrinhDuAn.MaCongTrinhDuAn";
                dt = cn.getTable(sql);
                if (dt.Rows.Count > 0)
                {
                    DateTime dtNgayLap = DateTime.Parse(dt.Rows[0]["NgayLap"].ToString());
                    Ngay = dtNgayLap.Day.ToString();
                    Thang = dtNgayLap.Month.ToString();
                    Nam = dtNgayLap.Month.ToString();
                    NguoiLap=dt.Rows[0]["NguoiLap"].ToString();
                    CongTrinhDuAn=dt.Rows[0]["TenCongTrinhDuAn"].ToString();
                }

                sql = @"SELECT  tbl_ChiTietPhieuYeuCauNhap.MaVatTu, dbo.tbl_VatTu.TenVatTu, dbo.tbl_DonViTinh.TenDonViTinh, dbo.tbl_ChiTietPhieuYeuCauNhap.SoLuong
                               FROM         dbo.tbl_ChiTietPhieuYeuCauNhap INNER JOIN
                                                      dbo.tbl_DonViTinh ON dbo.tbl_ChiTietPhieuYeuCauNhap.MaDonViTinh = dbo.tbl_DonViTinh.MaDonViTinh INNER JOIN
                                                      dbo.tbl_VatTu ON dbo.tbl_ChiTietPhieuYeuCauNhap.MaVatTu = dbo.tbl_VatTu.MaVatTu
                                WHERE tbl_ChiTietPhieuYeuCauNhap.SoPhieu='YC001'";
                dt = cn.getTable(sql);
                int stt=0;
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        stt++;
                        PhieuYeuCauNhap pyc = new PhieuYeuCauNhap();
                        pyc.SoPhieu=SoPhieu;
                        pyc.Ngay = Ngay;
                        pyc.Thang = Thang;
                        pyc.Nam = Nam;
                        pyc.NguoiLap = NguoiLap;
                        pyc.MaCongTrinhDuAn = CongTrinhDuAn;

                        pyc.STT = stt.ToString();
                        pyc.MaVatTu=dr["MaVatTu"].ToString();
                        pyc.TenVatTu=dr["TenVatTu"].ToString();
                        pyc.DVT=dr["TenDonViTinh"].ToString();
                        pyc.SoLuong=dr["SoLuong"].ToString();
                        list.Add(pyc);
                    }
                }
                frm.phieuYeuCauNhapBindingSource.DataSource = list;
                frm.ShowDialog();
            }
        }

        private void btnDuyet_Click(object sender, EventArgs e)
        {
            if (btnDuyet.Text == "Phê duyệt" && dgvPhieuYeuCauNhap.SelectedRows.Count > 0 && dgvPhieuYeuCauNhap.CurrentRow.Cells["colTrangThai"].Value.ToString() == "Chưa phê duyệt")
            {
                DialogResult msg = MessageBox.Show("Bạn có chắc chắn muốn phê duyệt phiếu?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                {
                    if (msg == DialogResult.Yes)
                    {
                        byte TrangThai = 1;
                        cn.Update_Table("tbl_PhieuYeuCauNhap", "TrangThai='" + TrangThai + "'", "SoPhieu='" + dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                    }
                }
            }
            if (btnDuyet.Text == "Hủy duyệt" && dgvPhieuYeuCauNhap.SelectedRows.Count > 0 && dgvPhieuYeuCauNhap.CurrentRow.Cells["colTrangThai"].Value.ToString() == "Đã phê duyệt")
            {
                DialogResult msg = MessageBox.Show("Bạn có chắc chắn muốn hủy phê duyệt phiếu?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                {
                    if (msg == DialogResult.Yes)
                    {
                        byte TrangThai = 0;
                        cn.Update_Table("tbl_PhieuYeuCauNhap", "TrangThai='" + TrangThai + "'", "SoPhieu='" + dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                    }
                }
            }
            load_dgvPhieuYeuCauNhap();
        }

        private void btnPhieuNhapKho_Click(object sender, EventArgs e)
        {
            if (dgvPhieuYeuCauNhap.SelectedRows.Count > 0)
            {
                if (dgvPhieuYeuCauNhap.CurrentRow.Cells["colTrangThai"].Value.ToString() == "Chưa phê duyệt")
                {
                    MessageBox.Show("Phiếu dự trù này chưa được phê duyệt.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                else
                {
                    frmPhieuNhapMua frm = new frmPhieuNhapMua();
                    frm.PhieuYeuCauNhap = dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString();
                    frm.ShowDialog();
                }
            }
            else
            {
                MessageBox.Show("Chưa chọn phiếu yêu cầu nhập.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }

        private string XacDinhDieuKienLoc()
        {
            string dieuKienLoc = "where 1=1";

            if (ckbTimTheoNgay.Checked == true)
            {
                dieuKienLoc += "and tbl_PhieuYeuCauNhap.NgayLap between '"+dtipTuNgay.Value.ToString()+"' and '"+dtipDenNgay.Value.ToString()+"'";
            }

            if (ckbSoPhieu.Checked == true && txtSoPhieu.Text != "")
            {
                dieuKienLoc += "and tbl_PhieuYeuCauNhap.SoPhieu like '%"+txtSoPhieu.Text+"%'";
            }

            if (ckbCongTrinhDuAn.Checked == true)
            {
                dieuKienLoc += "and tbl_PhieuYeuCauNhap.MaCongTrinhDuAn='"+cbbCongTrinhDuAn.SelectedValue.ToString()+"'";
            }

            if (ckbNhanVien.Checked == true && txtNhanVien.Text!="")
            {
                dieuKienLoc += "and tbl_PhieuYeuCauNhap.NguoiLap like '%"+txtNhanVien.Text+"%'";
            }

            if (ckbTimTheoTrangThai.Checked == true)
            {
                dieuKienLoc += "and tbl_phieuYeuCauNhap.TrangThai='"+cbbTrangThai.SelectedValue.ToString()+"'";
            }

            return dieuKienLoc;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            load_dgvPhieuYeuCauNhap(XacDinhDieuKienLoc());
        }

        private void radLoad_CheckedChanged(object sender, EventArgs e)
        {
            if (radLoad.Checked == true)
            {
                load_dgvPhieuYeuCauNhap();
                radLoad.Checked = false;
                ckbTimTheoNgay.Checked = false;
                ckbSoPhieu.Checked = false;
                ckbCongTrinhDuAn.Checked = false;
                ckbNhanVien.Checked = false;
                ckbTimTheoTrangThai.Checked = false;
            }
        }

        private void ckbTimTheoNgay_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbTimTheoNgay.Checked == true)
            {
                dtipTuNgay.Enabled = true;
                dtipDenNgay.Enabled = true;
            }
            else
            {
                dtipTuNgay.Enabled = false;
                dtipDenNgay.Enabled = false;
            }
        }

        private void ckbSoPhieu_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbSoPhieu.Checked == true)
            {
                txtSoPhieu.Text = "";
                txtSoPhieu.Enabled = true;
            }
            else
            {
                txtSoPhieu.Text = "Tìm theo số phiếu";
                txtSoPhieu.Enabled = false;
            }
        }

        private void ckbCongTrinhDuAn_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbCongTrinhDuAn.Checked == true)
            {
                cbbCongTrinhDuAn.Enabled = true;
            }
            else
            {
                cbbCongTrinhDuAn.Enabled = false;
            }
        }

        private void ckbNhanVien_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNhanVien.Checked == true)
            {
                txtNhanVien.Text = "";
                txtNhanVien.Enabled = true;
            }
            else
            {
                txtNhanVien.Text = "Tìm theo người lập phiếu";
                txtNhanVien.Enabled = false;
            }
        }

        private void ckbTimTheoTrangThai_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbTimTheoTrangThai.Checked == true)
            {
                cbbTrangThai.Enabled = true;
            }
            else
            {
                cbbTrangThai.Enabled = false;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvPhieuYeuCauNhap.SelectedRows.Count > 0 && dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value != null)
            {
                bool TrangThai = bool.Parse(cn.LayDuLieu("SELECT [TrangThai] FROM [tbl_PhieuYeuCauNhap] WHERE [SoPhieu] = '" + dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'"));
                if (TrangThai == true)
                {
                    MessageBox.Show("Bạn không thể xóa phiếu yêu cầu đã được ký duyệt!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    DialogResult msg = MessageBox.Show("Bạn chắc chắn muốn xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (msg == DialogResult.Yes)
                    {
                        cn.Delete_Table("tbl_ChiTietPhieuYeuCauNhap", "SoPhieu='" + dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                        cn.Delete_Table("tbl_PhieuYeuCauNhap", "SoPhieu='" + dgvPhieuYeuCauNhap.CurrentRow.Cells["colSoPhieu"].Value.ToString() + "'");
                        MessageBox.Show("Xóa thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            load_dgvPhieuYeuCauNhap();
        }
    }
}
